package com.gezi.admin.security.filter;

import com.gezi.admin.security.utils.ResponseUtil;
import com.gezi.admin.module.config.utils.ResultCodeE;
import com.gezi.admin.module.config.utils.ResultUtil;
import com.gezi.admin.security.exception.LoginAuthenticationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 格子软件
 * @createDate 2024/2/10 09:00
 * @contact 公众号：格子软件 微信：13716343106 邮箱：1424529270@qq.com
 */
@Slf4j
public class AuthenticationHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        authException.printStackTrace();
        response.setStatus(200);
        if(authException instanceof LoginAuthenticationException){
            LoginAuthenticationException loginAuthenticationException = (LoginAuthenticationException) authException;
            ResponseUtil.out(response, ResultUtil.error(loginAuthenticationException.getCode(),loginAuthenticationException.getChnDesc()));
        }else{
            ResponseUtil.out(response, ResultUtil.error(ResultCodeE.SYS_ERROR.getCode(),authException.getMessage()));
        }
    }
}
